package com.ucs.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ucs.domain.Order;
import com.ucs.util.UCSData;

public class ReviewPendingOrderActivity extends SimpleActivity 
	implements AdapterView.OnItemClickListener, SimpleActivity.ResultCallbackIF{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.d("ucs", "+ReviewPendingOrderActivity.onCreate()");
		setContentView(R.layout.review_pending_orders);
				
		ListView order_listview = (ListView)findViewById(R.id.orders_list);
		setHeader(order_listview);
//		UCSData.getInstance().printPendingOrderList();
		order_listview.setAdapter(UCSData.getInstance().getPendingOrderListAdaptor(this));
		
		order_listview.setOnItemClickListener(this);
		Log.d("ucs", "-ReviewPendingOrderActivity.onCreate()");
		
	}

	private void setHeader(ListView order_listview) {
		LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View convertView = li.inflate(R.layout.order_row, null);
		convertView.setBackgroundColor(Color.BLUE);
    	TextView ticket_num = (TextView)convertView.findViewById(R.id.ticket_num);
    	TextView table_id = (TextView)convertView.findViewById(R.id.table_id);
    	TextView table_desc = (TextView)convertView.findViewById(R.id.table_desc);

    	ticket_num.setText("Ticket");
    	table_id.setText("Table");
    	table_desc.setText("Total Items");
    	order_listview.addHeaderView(convertView, null, false);
    	order_listview.setHeaderDividersEnabled(true);
	}

	private Order pendingOrder;
	private int position;
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		try {
			Log.d("ucs", "+ReviewPendingOrderActivity:onItemClick");
			Bundle extras = new Bundle();
			position = arg2-1;
			pendingOrder = UCSData.getInstance().peekPendingOrder(position);
			if (pendingOrder.getSend() == 1){
				Toast.makeText(this, "The system is trying to send order. Please select another order", Toast.LENGTH_SHORT);
				return;
			}
			UCSData.getInstance().removePendingOrder(position);
			Log.d("ucs", "table="+pendingOrder.getTableID()+", no_of_items="+pendingOrder.getOrderDetails().size());
			extras.putSerializable(NewOrderActivity.PENDING_ORDER, pendingOrder);
			launchSubActivity(NewOrderActivity.class, this, extras);			
			Log.d("ucs", "-ReviewPendingOrderActivity:onItemClick");
		} catch (IndexOutOfBoundsException ioe) {
			Toast.makeText(this, "The selected pending order has already been submitted", Toast.LENGTH_LONG);
		}
	}

	@Override
	public void resultCancel(Intent intent) {
		Log.d("ucs", "+ReviewPendingOrderActivity.resultCancel");		
		Order modifiedOrder = NewOrderActivity.getCreatedOrder(intent);
		if (modifiedOrder != null){
			pendingOrder = modifiedOrder;
		}
		UCSData.getInstance().addPendingOrder(position, pendingOrder);
		Log.d("ucs", "-ReviewPendingOrderActivity.resultCancel");		
	}


	@Override
	public void resultOk(Intent intent) {
		Log.d("ucs", "+ReviewPendingOrderActivity.resultOk");
		Log.d("ucs", "-ReviewPendingOrderActivity.resultOk");		
	}
	
}
